<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              label-width="120px"
              style="margin-left: 120px; margin-top:30px"
              :model="userInfoForm"
              :rules="rules"
              ref="userInfoForm"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input style="width:300px" v-model="userInfoForm.username"/>
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input style="width:300px" type="password" v-model="userInfoForm.password2"/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserDetailById">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${id}?type=personal`">
                  <i class="el-icon-printer"/>
                </router-link>
              </el-tooltip>
            </el-row>
            <user-info :userId="id"></user-info>
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${id}?type=job`">
                  <i class="el-icon-printer"/>
                </router-link>
              </el-tooltip>
            </el-row>
            <job-info :userId="id"></job-info>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getUserDetailById, saveUserDetailById } from "@/api/user";
import userInfo from "./components/user-info";
import jobInfo from "./components/job-info";
export default {
  components: {
    userInfo,
    jobInfo
  },
  created() {
    this.getUserDetailById();
  },
  data() {
    return {
      id: this.$route.params.id,
      userInfoForm: {
        username: "",
        password2: ""
      },
      rules: {
        username: [
          { required: true, message: "请填写用户名", trigger: "blur" }
        ],
        password2: [
          { required: true, message: "请填写密码", trigger: "blur" },
          { max: 12, min: 6, message: "密码长度在6-12位", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    async getUserDetailById() {
      try {
        this.userInfoForm = await getUserDetailById(this.id);
      } catch (err) {}
    },
    saveUserDetailById() {
      this.$refs.userInfoForm.validate(async valid => {
        if (valid) {
          try {
            await saveUserDetailById({
              username: this.userInfoForm.username,
              password: this.userInfoForm.password2,
              id: this.id
            });
            this.userInfoForm = {};
            this.$message.success("修改成功");
            this.getUserDetailById();
          } catch (err) {}
        } else {
          return false;
        }
      });
    }
  }
};
</script>
